<template>
    <div class="tag-lists">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="iconfont icon-wenzhangliebiaoxiangqing"></i> 标签管理</el-breadcrumb-item>
                <el-breadcrumb-item>标签列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-table
                    :data="tableData"
                    style="width: 100%">
                <el-table-column type="expand">
                    <template slot-scope="props" v-if="props.row.children.length">
                            <el-table
                                    :data="props.row.children"
                                    style="width: 100%">
                                <el-table-column
                                        type="index"
                                        width="50">
                                </el-table-column>
                                <el-table-column
                                        prop="name"
                                        label="分类名称"
                                        >
                                </el-table-column>
                                <el-table-column
                                        prop="created_at"
                                        label="创建时间">
                                </el-table-column>
                                <el-table-column
                                        label="操作">
                                    <template slot-scope="scope">
                                        <el-button
                                                size="mini"
                                                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                                        <el-button
                                                size="mini"
                                                type="danger"
                                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                    </template>
                </el-table-column>
                <el-table-column
                        label="id"
                        prop="id">
                </el-table-column>
                <el-table-column
                        label="标签名称"
                        prop="name">
                </el-table-column>
                <el-table-column
                        label="创建时间"
                        prop="created_at">
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button v-if="!scope.row.children.length"
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>

            </el-table>
        </div>

        <el-dialog title="编辑分类"  :visible.sync="dialogFormVisible">
            <EditTag @editSuccess="editSuccess" v-if="dialogFormVisible" :tag_id="tag_id"/>
        </el-dialog>
    </div>
</template>

<script>
    import EditTag from './EditArticleTag'
    export default {
        name: "article-tag-lists",
        data(){
            return {
                tableData: [],
                dialogFormVisible:false,
                tag_id:0,
            }
        },
        components:{
            EditTag
        },
        created(){
          this.getTags();
        },
        methods:{
            editSuccess(){
                this.dialogFormVisible = false;
                this.getTags();
            },
            getTags(){
              this.getAjax('admin/get_tags').then(res=>{
                  if(res.code){
                      this.tableData = res.data;
                  }
              })
            },
            handleClick(item){
                console.log(item);
            },
            editItem(item){
                console.log(item);
            },
            handleEdit(index,obj){
                this.dialogFormVisible = true;
                this.tag_id = obj.id;
                console.log(this.tag_id);
                // this.$router.push({path:'/editTag',query:{tag_id:obj.id}})
            },
            handleDelete(index,obj){

                this.$confirm('是否删除此标签？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    this.getAjax('admin/remove_tag',{id:obj.id}).then(res=>{
                        this.$message.success(res.msg);
                        if(res.code){
                            this.getTags();
                        }
                    });
                }).catch(()=>{
                    console.log('取消');
                })


            }
        }
    }
</script>

<style scoped lang="scss">
    .two_level_text{
        margin-right: 10px;
    }
</style>
